package com.example.pilipili.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.dto.UInfo;
import com.example.pilipili.pojo.entity.User;
import com.example.pilipili.service.UserService;
import com.example.pilipili.utils.FileU;
import com.example.pilipili.utils.JwtU;
import com.example.pilipili.utils.MD5U;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService ;


    /**
     * 登录
     * @param user phone,password
     * @param session
     * @return user
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody @Valid  User user, HttpSession session) {
        QueryWrapper<User> qw = new QueryWrapper<>() ;
        qw.eq("phone",user.getPhone()).eq("password",MD5U.getMD5Password(user.getPassword())) ;
        User user1 = userService.getOne(qw) ;
        if(user1 == null) {
            return R.failed("账号或密码错误");
        }
        user1.setPassword(null);
        Map<String, Object> info = new HashMap<>();
        info.put("name", user1.getName());
        session.setAttribute("token", JwtU.sign(String.valueOf(user.getId()),info ));
        return R.ok(user1) ;
    }

    /**
     * 注册
     * @param user username,phone,password
     * @return null
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid User user) {
        return userService.register(user);
    }

    /**
     * 更改基本信息
     * @param user id,username,gender,card
     * @return user
     */
    @PutMapping
    public R<User> updateBaseInfo(@RequestBody @Valid User user) {
        return userService.updateBaseInfo(user) ;
    }

    /**
     * 根据 uid 获取用户基本信息
     * @param uid uid
     * @return UInfo
     */
    @GetMapping("/info/{uid}")
    public R<UInfo> getUInfoById(@PathVariable int uid){
        return userService.selectInfoById(uid) ;
    }

    @PostMapping("/aver")
    public R<User> changeHandUrl(@RequestParam("aver")MultipartFile file,@RequestParam("uid")int uid) {
        return userService.changeHandUrl(uid, FileU.uploadFileToLocal(file,"avers")) ;
    }

    /**
     * 根据 status 获取用户
     * @param status status
     * @return list user
     */
    @GetMapping("/s/{status}")
    public R<List<User>> getsByStatus(@PathVariable int status){
        return userService.getsByStatus(status) ;
    }

    /**
     * 更改用户状态
     * @param user id，status
     * @return user
     */
    @PostMapping("/s")
    public R<User> changeStatus(@RequestBody @Valid User user) {
        return userService.updateStatus(user) ;
    }

    /**
     * 根据 phone 查询
     * @param phone phone
     * @return user
     */
    @GetMapping("/p")
    public R<List<User>> getsByPhone(@RequestParam("phone")  String phone){
        QueryWrapper<User> qw = new QueryWrapper<>() ;
        qw.like("phone",phone) ;
        return R.ok(userService.list(qw)) ;
    }



}

